package com.jy.wms.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.jy.wms.common.PinyinUtils;
import com.jy.wms.common.SetStatus;
import com.jy.wms.common.base.BaseService;
import com.jy.wms.common.base.ServiceException;
import com.jy.wms.dao.WmsProvinceRankingReportMapper;
import com.jy.wms.pojo.*;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.jeecgframework.poi.excel.entity.params.ExcelExportEntity;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;

import static com.jy.wms.common.base.BaseController.getJSONObject;

//报表导出
@Service
public class WmsSupportNumberReportExportService extends BaseService<WmsProvinceRankingReportMapper, WmsProvinceRankingReport> {




    /**
     * 直销省市排名报表导出（根据时间查询订单数量）
     * @return List<WmsProvinceRankingReport1>
     */
    public List<WmsProvinceRankingReport> baseselectbathprimarykeys(WmsProvinceRankingReport WmsProvinceRankingReport,String hzid,String ckid) {

        List<String> ss=WmsProvinceRankingReport.getIds();
        List<WmsProvinceRankingReport> Sales = dao.baseselectbyprimaryExport(WmsProvinceRankingReport);

        int i = 1;//记录排名
        int sddl = 0;//记录订单量
        int zddl = 0;
        double Ddslbfb = 0;
        int zxse = 0;
        int zddbfb = 0;
        for (WmsProvinceRankingReport user : Sales) {

            //订单量排序
            if (sddl == 0) {
                sddl = Integer.parseInt(user.getDdl());
                i++;
            } else {
                if (Integer.parseInt(user.getDdl()) == sddl) {
                } else {
                    sddl = Integer.parseInt(user.getDdl());
                    i++;
                }
            }
            //总计
            if (user.getDdl() != null) {
                //统计订单量总计
                int ddL = Integer.parseInt(user.getDdl());
                zddl += ddL++;
            }
            String str = user.getDdlbfb().substring(0, user.getDdlbfb().length() - 1);
            double DdslbfB = Double.parseDouble(str);
            Ddslbfb += DdslbfB;
            //统计总销售额
            if (user.getXse() != null) {
                int zxsE = Integer.parseInt(user.getXse());
                zxse += zxsE;
            }
            if (user.getXsezb() != null) {
                //统计总销售额百分比
                String jq = user.getDdlbfb().substring(0, user.getDdlbfb().length() - 1);
                double zddbfB = Double.parseDouble(jq);
                zddbfb += zddbfB;
            }
        }
        return Sales;
    }


    /**
     * 各省运费统计（按产品大类）
     *
     * @param
     * @return list
     */
    public List<Map<String, Object>> StatisticsInVariousProvinces(WmsSummaryOfTheSituationInEachProvince WmsSummaryOfTheSituationInEachProvince) {

        PinyinUtils pinyin = new PinyinUtils();
        Map<String, Object> map = null;
        //String state = (String) mapp.get("state");
        String state = WmsSummaryOfTheSituationInEachProvince.getState();
        String ckid=WmsSummaryOfTheSituationInEachProvince.getCkid();
        String hzid=WmsSummaryOfTheSituationInEachProvince.getHzid();
        List<Map<String, Object>> Thenumber = new Page<>();
        List list;
        if (state != null && state != "") {
            //单独查询出省
            list = dao.freightStatisticsInVariousExport(WmsSummaryOfTheSituationInEachProvince);

            //取出种类
            List<WmsHead> Frei = dao.WmsFreightHeadExport(WmsSummaryOfTheSituationInEachProvince);
            int d = 1;//累加
            //Map<String, Object> zhh = new LinkedHashMap<>();
            String sheng = null;
            int zzl = 0;//该省的所有商品的总量
            double yf = 0.0;//单个运费
            double zyf = 0.0;//每个省的运费汇总
            double ZZ = 0.0;//求百分比
            String dgzl = null;//记录每个种类

            List<WmsFreightStatisticsInVariousProvinces> Sales = null;
            for (int str = 0; str < list.size(); str++) {
                map = new LinkedHashMap<>();
                for (WmsHead use : Frei) {
                    String type = use.getMc();
                    map.put("id",use.getId());
                    Sales = dao.freightStatisticsInVariousProvinces(list.get(str) + "", state, type,hzid,ckid);
                    if (Sales.isEmpty()) {
                        map.put("Province", list.get(str));
                        //map.put("Categoryname" + d++, type);
                        String ty = pinyin.ToPinyin(type);
                        map.put(ty, 0);
                    }
                    for (WmsFreightStatisticsInVariousProvinces user : Sales) {
                        //map.put("Categoryname" + d++, user.getMc());
                        dgzl = user.getMc();
                        //判断是否是同一个种类
                        if (user.getMc().equals(dgzl)) {
                            //取出每个种类的重量信息
                            double Z = Double.parseDouble(user.getMzzl());
                            zzl += Z;
                        } else if (user.getMc() != dgzl) {
                            dgzl = "";
                            zzl = 0;
                            //z=0;
                            //取出每个种类的重量信息
                            double Z = Double.parseDouble(user.getMzzl());
                            zzl += Z;

                        }

                        if (zzl == 0 || user.getMzzl().equals(0.00)) {
                            //返回0
                            yf = 0;
                            String ty = pinyin.ToPinyin(user.getMc());
                            map.put(ty, yf + "");
                        } else {
                            //类型进行转换
                            double YDSL = Double.parseDouble(user.getMzzl());
                            //进行百分比运算
                            ZZ = YDSL / zzl;
                            double ZYF = Double.parseDouble(user.getZyf());
                            yf = ZYF * ZZ;
                            BigDecimal b = new BigDecimal(yf);
                            yf = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                            String ty = pinyin.ToPinyin(user.getMc());
                            map.put(ty, yf + "");
                        }

                        if (sheng == null) {
                            sheng = user.getSheng();
                            map.put("Province", list.get(str));
                            //每个省的运费汇总
                            zyf += yf;
                            BigDecimal b = new BigDecimal(zyf);
                            zyf = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                            map.put("FreightCollection", zyf + "");
                        } else {
                            if (sheng.equals(user.getSheng())) {
                                //每个省的运费汇总
                                zyf += yf;

                                BigDecimal b = new BigDecimal(zyf);
                                zyf = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                                map.put("FreightCollection", zyf + "");
                            } else if (sheng != user.getSheng()) {
                                zyf = 0.0;
                                sheng = user.getSheng();
                                map.put("Province", list.get(str));
                                //每个省的运费汇总
                                zyf += yf;
                                BigDecimal b = new BigDecimal(zyf);
                                zyf = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                                map.put("FreightCollection", zyf + "");
                            }
                        }
                    }
                }
                Thenumber.add(map);
            }
        }
        return Thenumber;
    }

    //运费导出报表
    public List<ExcelExportEntity> listHeader2(WmsSummaryOfTheSituationInEachProvince WmsSummaryOfTheSituationInEachProvince) {
        List<ExcelExportEntity> entity = new ArrayList<ExcelExportEntity>();
        //String state = (String) mapp.get("state");

        //添加省份
        String sheng=WmsSummaryReportsOnTransportation.Province.getType();
        //拼音
        //String cpbh2=PinyinUtils.ToPinyin(sheng);
        entity.add(new ExcelExportEntity(sheng, "Province"));

        //添加运费汇总
        String huizong=WmsSummaryReportsOnTransportation.FreightCollection.getType();
        //拼音
        //String cpbh3=PinyinUtils.ToPinyin(huizong);
        entity.add(new ExcelExportEntity(huizong, "FreightCollection"));

        List<WmsHead> Frei=dao.WmsFreightHeadExport(WmsSummaryOfTheSituationInEachProvince);
        for (WmsHead use : Frei) {
            String mc=use.getMc();
            //拼音
            String ddlx2=PinyinUtils.ToPinyin(mc);
            entity.add(new ExcelExportEntity(mc, ddlx2));
        }
        return entity;
    }

    /**
     * 各省发货支数统计（按产品大类）
     *
     * @param
     * @return list
     */
    public List<Map<String, Object>> pmentsineachprovince(Map<String, Object> mapp,String hzid,String ckid) {

        PinyinUtils pinyin = new PinyinUtils();
        JSONObject JSON = new JSONObject();
        Map<String, Object> map;
        //添加总计
        Map<String, Object> zhh = new LinkedHashMap<>();
        String state = (String) mapp.get("state");
        List<Map<String, Object>> Thenumber = new Page<>();

        if (state != null && state != "") {
            //取出所有省份
            List list = dao.freightStatistics(state,hzid,ckid);
            //取出所有种类
            List<WmsHead> su = dao.supportnumberhead(state,hzid,ckid);

            String sheng = null;//判断省份
            int mc = 0;//定义总计
            int zzl = 0;//该省的支数汇总
            List<WmsShipmentsInEachProvince> Sales = null;
            for (int str = 0; str < list.size(); str++) {
                map = new LinkedHashMap<>();
                for (WmsHead use : su) {
                    String type = use.getMc();
                    Sales = dao.thenumberofshipmentsineachprovince(list.get(str) + "", state, type,hzid,ckid);
                    if (Sales.isEmpty()) {
                        map.put("Province", list.get(str));
                        //map.put("Categoryname" + d++, type);
                        String ty = pinyin.ToPinyin(type);
                        map.put(ty, 0);
                    }
                    for (WmsShipmentsInEachProvince user : Sales) {
                        //map.put("Categoryname" + d++, user.getMc());
                        String ty = pinyin.ToPinyin(user.getMc());
                        map.put(ty, user.getZs());
                        if (sheng == null) {
                            sheng = user.getSheng();
                            map.put("Province", list.get(str));
                            int zs = Integer.parseInt(user.getZs());
                            map.put("SupportNumberSummary", zzl += zs);
                        } else {
                            if (sheng.equals(user.getSheng())) {
                                int zs = Integer.parseInt(user.getZs());
                                map.put("SupportNumberSummary", zzl += zs);
                            } else if (sheng != user.getSheng()) {
                                zzl = 0;
                                sheng = user.getSheng();
                                map.put("Province", list.get(str));

                                int zs = Integer.parseInt(user.getZs());
                                map.put("SupportNumberSummary", zzl += zs);
                            }
                        }
                    }
                }
                Thenumber.add(map);
            }
            Thenumber.add(zhh);
        }
        return Thenumber;
    }

    public List<ExcelExportEntity> zishuHeader(Map<String, Object> mapp,String hzid, String ckid) {
        List<ExcelExportEntity> entity = new ArrayList<ExcelExportEntity>();
        String state = (String) mapp.get("state");

        //添加省份
        String sheng=WmsSummaryReportsOnTransportation.Province.getType();
        //拼音
        //String cpbh2=PinyinUtils.ToPinyin(sheng);
        entity.add(new ExcelExportEntity(sheng, "Province"));
        //添加支数汇总
        String huizong=WmsSummaryReportsOnTransportation.SupportNumberSummary.getType();
        entity.add(new ExcelExportEntity(huizong, "SupportNumberSummary"));

        //取出所有种类
        List<WmsHead> su = dao.supportnumberhead(state,hzid,ckid);
        for (WmsHead use : su) {
            String mc=use.getMc();
            //拼音
            String ddlx2=PinyinUtils.ToPinyin(mc);
            entity.add(new ExcelExportEntity(mc, ddlx2));
        }

        return entity;
    }

    /**
     * 各省销售额统计（按产品大类）
     *
     * @param
     * @return list
     */
    public List<Map<String, Object>> StatisticsOfTheBranches(Map<String, Object> mapp, String hzid, String ckid) {

        JSONObject JSON = new JSONObject();
        PinyinUtils pinyin = new PinyinUtils();
        Map<String, Object> map;
        Map<String, Object> zhh = new LinkedHashMap<>();

        String state = (String) mapp.get("state");
        List<Map<String, Object>> Thenumber = new Page<>();

        if (state != null && state != "") {
            //单独查询出省
            List list = dao.fsalesineachprovince(state,hzid,ckid);
            //单独查询出种类
            List<WmsHead> Salesvolume = dao.Saleshead(state,hzid,ckid);

            String mc = null;//判断规格
            String sheng = null;//判断省份
            double xse = 0.0;
            List<WmsStatisticsoFsalesIneachProvince> Sales = null;
            //将省份进行遍历
            for (int str = 0; str < list.size(); str++) {

                map = new HashMap<>();
                for (WmsHead use : Salesvolume) {

                    String type = use.getMc();
                    //根据指定省份查询出销售额
                    Sales = dao.statisticsofsalesineachprovince(list.get(str) + "", state, type,hzid,ckid);

                    if (Sales.isEmpty()) {
                        //map.put("Categoryname" + d++, type);

                        String ty = pinyin.ToPinyin(type);
                        map.put(ty, 0);
                        map.put("Province", list.get(str));
                    }
                    for (WmsStatisticsoFsalesIneachProvince user : Sales) {
                        //map.put("Categoryname" + d++, user.getMc());
                        double Z = Double.parseDouble(user.getHpje());
                        BigDecimal b = new BigDecimal(Z);
                        Z = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                        String ty = pinyin.ToPinyin(user.getMc());
                        map.put(ty, Z);


                        if (sheng == null) {
                            sheng = user.getSheng();
                            map.put("Province", list.get(str));
                            double je = Double.parseDouble(user.getHpje());
                            xse += je;
                            BigDecimal B = new BigDecimal(xse);
                            xse = B.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                            map.put("SalesVolumeSummary", xse);
                        } else {
                            if (sheng.equals(user.getSheng())) {
                                double je = Double.parseDouble(user.getHpje());
                                xse += je;
                                BigDecimal B = new BigDecimal(xse);
                                xse = B.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                                map.put("SalesVolumeSummary", xse);
                            } else if (sheng != user.getSheng()) {
                                xse = 0.0;
                                sheng = user.getSheng();
                                map.put("Province", list.get(str));

                                double je = Double.parseDouble(user.getHpje());
                                xse += je;
                                BigDecimal B = new BigDecimal(xse);
                                xse = B.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                                map.put("SalesVolumeSummary", xse);
                            }
                        }
                    }
                }
                Thenumber.add(map);
            }
            Thenumber.add(zhh);
        }
        return Thenumber;
    }

    public List<ExcelExportEntity> xseHeader(Map<String, Object> mapp,String hzid, String ckid) {
        List<ExcelExportEntity> entity = new ArrayList<ExcelExportEntity>();
        String state = (String) mapp.get("state");

        //添加省份
        String sheng=WmsSummaryReportsOnTransportation.Province.getType();
        //拼音
        //String cpbh2=PinyinUtils.ToPinyin(sheng);
        entity.add(new ExcelExportEntity(sheng, "Province"));

        //添加销售额汇总
        String huizong=WmsSummaryReportsOnTransportation.SalesVolumeSummary.getType();
        entity.add(new ExcelExportEntity(huizong, "SalesVolumeSummary"));

        //取出所有种类
        List<WmsHead> Salesvolume = dao.Saleshead(state,hzid,ckid);
        for (WmsHead use : Salesvolume) {
            String mc=use.getMc();
            //拼音
            String ddlx2=PinyinUtils.ToPinyin(mc);
            entity.add(new ExcelExportEntity(mc, ddlx2));
        }

        return entity;
    }

    /**
     * 各省订单量统计（按产品大类）
     *
     * @param
     * @return list
     */
    public List<Map<String, Object>> OrderQuantityStatistics(Map<String, Object> mapp, String hzid, String ckid) {
        JSONObject JSON = new JSONObject();
        PinyinUtils pinyin = new PinyinUtils();
        Map<String, Object> map;
        Map<String, Object> zhh = new LinkedHashMap<>();

        String state = (String) mapp.get("state");
        List<Map<String, Object>> Thenumber = new Page<>();

        if (state != null && state != "") {
            //单独查询出省
            List list = dao.statisticsoforderquantity(state,hzid,ckid);
            //查出所有种类
            List<WmsHead> Order = dao.Orderquantityheader(state,hzid,ckid);
            String sheng = "";//判断省份
            int zzl = 0;//该省的所有商品的总销售额
            List<WmsShipmentsInEach> Sales = null;
            try {
                for (int str = 0; str < list.size(); str++) {
                    map = new HashMap<>();
                    for (WmsHead use : Order) {
                        String type = use.getMc();
                        Sales = dao.statisticsoforderquantityineachprovince(list.get(str) + "", state, type,hzid,ckid);
                        if (Sales.isEmpty()) {
                            //map.put("Categoryname" + d++, type);
                            map.put("Province", list.get(str));
                            String ty = pinyin.ToPinyin(type);
                            map.put(ty, 0);
                        }
                        for (WmsShipmentsInEach user : Sales) {
                            //map.put("Categoryname" + d++, user.getMc());
                            String ty = pinyin.ToPinyin(user.getMc());
                            map.put(ty, user.getFybh());

                            if (sheng == "") {
                                sheng = user.getSheng();
                                map.put("Province", list.get(str));
                                int zs = Integer.parseInt(user.getFybh());
                                map.put("OrderQuantitySummary", zzl += zs);
                            } else {
                                if (sheng.equals(user.getSheng())) {
                                    int zs = Integer.parseInt(user.getFybh());
                                    map.put("OrderQuantitySummary", zzl += zs);
                                } else if (sheng != user.getSheng()) {
                                    String ss = list.get(str).toString();
                                    if (ss.equals("")) {
                                        int zs = Integer.parseInt(user.getFybh());
                                        map.put("OrderQuantitySummary", zzl += zs);
                                    } else {
                                        zzl = 0;
                                        sheng = user.getSheng();
                                        map.put("Province", list.get(str));
                                        int zs = Integer.parseInt(user.getFybh());
                                        map.put("OrderQuantitySummary", zzl += zs);
                                    }
                                }
                            }
                        }
                    }
                    Thenumber.add(map);
                }
            } catch (Exception e) {
                e.getMessage();
            }
        }
        return Thenumber;
    }

    /**
     * 各省订单量统计（按产品大类）表头
     *
     * @param
     * @return list
     */
    public List<ExcelExportEntity> ddlHeader(Map<String, Object> mapp,String hzid, String ckid) {
        List<ExcelExportEntity> entity = new ArrayList<ExcelExportEntity>();
        String state = (String) mapp.get("state");

        //添加省份
        String sheng=WmsSummaryReportsOnTransportation.Province.getType();
        //拼音
        //String cpbh2=PinyinUtils.ToPinyin(sheng);
        entity.add(new ExcelExportEntity(sheng, "Province"));

        //添加订单量汇总
        String huizong=WmsSummaryReportsOnTransportation.OrderQuantitySummary.getType();
        entity.add(new ExcelExportEntity(huizong, "OrderQuantitySummary"));
        //取出所有种类
        List<WmsHead> Order = dao.Orderquantityheader(state,hzid,ckid);
        for (WmsHead use : Order) {
            String mc=use.getMc();
            //拼音
            String ddlx2=PinyinUtils.ToPinyin(mc);
            entity.add(new ExcelExportEntity(mc, ddlx2));
        }

        return entity;
    }
}
